package datetime

import (
	"database/sql/driver"
	"time"
)

type Timestamp struct {
	Time time.Time
}

func (t *Timestamp) Scan(value interface{}) error {
	switch v := value.(type) {
	case time.Time:
		t.Time = v
	case int64, uint64:
		t.Time = time.Unix(value.(int64), 0).UTC()
	}
	return nil
}

func (t Timestamp) Value() (driver.Value, error) {
	if t.Time.IsZero() {
		return nil, nil
	}
	return t.Time.Unix(), nil
}

func Now() Timestamp {
	return Timestamp{Time: time.Now()}
}

func Of(t time.Time) Timestamp {
	return Timestamp{
		Time: t,
	}
}
